#region Using directives
using System;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
#endregion

namespace SharpWind.Graphics
{
	public class ScreenQuad
	{
		#region Fields

		private CustomVertex.TransformedTextured[] vertices = null;

		private VertexBuffer vb = null;

		#endregion

		#region Constructor 

		public ScreenQuad( int width, int height )
		{
			this.vertices = new CustomVertex.TransformedTextured[6];
			this.vertices[0] = new CustomVertex.TransformedTextured( new Vector4( -0.5f, -0.5f, 0f, 1f ), 0f, 0f );
			this.vertices[1] = new CustomVertex.TransformedTextured( new Vector4( width - 0.5f, -0.5f, 0f, 1f ), 1f, 0f );
			this.vertices[2] = new CustomVertex.TransformedTextured( new Vector4( -0.5f, height - 0.5f, 0f, 1f ), 0f, 1f );
			this.vertices[3] = new CustomVertex.TransformedTextured( new Vector4( width - 0.5f, -0.5f, 0f, 1f ), 1f, 0f );
			this.vertices[4] = new CustomVertex.TransformedTextured( new Vector4( width - 0.5f, height - 0.5f, 0f, 1f ), 1f, 1f );
			this.vertices[5] = new CustomVertex.TransformedTextured( new Vector4( -0.5f, height - 0.5f, 0f, 1f ), 0f, 1f );

			this.vb = new VertexBuffer( typeof( CustomVertex.TransformedTextured ), 6, Engine.Instance.Graphics.Device,
										Usage.WriteOnly, CustomVertex.TransformedTextured.Format, Pool.Managed );
			this.vb.Created += new EventHandler( vb_Created );
			vb_Created( this.vb, null );
		}

		#endregion

		#region Methods

		public void Render()
		{
			Engine.Instance.Graphics.Device.VertexFormat = CustomVertex.TransformedTextured.Format;
			Engine.Instance.Graphics.Device.SetStreamSource( 0, this.vb, 0 );
			Engine.Instance.Graphics.Device.DrawPrimitives( PrimitiveType.TriangleList, 0, 2 );
		}

		private void vb_Created( object sender, EventArgs e )
		{
			this.vb.SetData( this.vertices, 0, LockFlags.None );
		}

		#endregion

		#region Properties

		public VertexBuffer VertexBuffer
		{
			get
			{
				return this.vb;
			}
		}

		#endregion
	}
}
